function initPage(pageNo,totalCount,arr){
    // 获取分页器实例对象
    var myPager = $('#myPager').data('zui.pager');
    //设置当前页码为 4，并同时设置记录总数为 100， 每页记录数目为 30
    myPager.set(pageNo, totalCount, 5);
    renderSubscribeList(arr)
}
$('#myPager').pager({
    onPageChange: function(state, oldState) {
        if (state.page !== oldState.page) {
            queryBtn(state.page);
        }
    }
});
var dataArr;
//获取，渲染分页数据
function renderSubscribeList(arr){
    let str = '';
    for (let i = 0; i < arr.length; i++) {
        let a  = arr[i];
        let subStatus = a.subStatus;
        if (subStatus == '已预约' || subStatus == '已确认'){
            str += `<tr>
                <td>${a.subTime}</td>
                <td>${a.uusername}</td>
                <td>${a.fieldName}</td>
                <td>${a.questionInfo}</td>
                <td>${a.subStatus }</td>
                <td style="text-align: left">
                    <button onclick="sub(${a.id},'${subStatus}')" class="btn btn-danger sub-btn" data-toggle="" data-target="" style="width: 80px;">${subStatus == '已预约' ? '确认预约':'诊断'}</button>
                    <button onclick="subInfo(${a.id})" 
                    class="btn btn-success" data-toggle="modal"
                     data-target="#myModal2" 
                     style="width: 80px;" >查看详情</button>
                </td>
            </tr>`;
            $('#tbody')[0].innerHTML = str;
        }else{
            str += `<tr>
                <td>${a.subTime}</td>
                <td>${a.uusername}</td>
                <td>${a.fieldName}</td>
                <td>${a.questionInfo}</td>
                <td>${a.subStatus}</td>
                <td style="text-align: left">
                    <button onclick="subInfo(${a.id})" 
                    style="width: 80px;" type="button" 
                    data-toggle="modal" data-target="#myModal2" id="updateBtn"  
                    class="btn btn-success ">查看详情</button>
              
                </td>
            </tr>`;
            $('#tbody')[0].innerHTML = str;
        }
        // var str2 = '';
        // if (subStatus == '已确认'){
        //
        // }
        // $('#diagnosis-form')[0].innerHTML =
    }
}
function subInfo(id){

    console.log(id);
    $.ajax({
        url:config.url + "/order/subscribeInfoForOne",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            id:id
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            var consInfo = resp.data;
            var str = '';
            str+=
                `
                    <ul class="list-group">
                            <li class="list-group-item">咨询人:&nbsp;&nbsp;${consInfo.username}</li>
                            <li class="list-group-item">领域:  ${consInfo.fieldName}</li>
                            <li class="list-group-item">预约时间:&nbsp;&nbsp;${consInfo.subTime}</li>
                            <li class="list-group-item">预约状态:&nbsp;&nbsp;${consInfo.subStatus}</li>
                            <li class="list-group-item">费用:&nbsp;&nbsp;${consInfo.cost}</li>
                            <li class="list-group-item">咨询师:&nbsp;&nbsp;${consInfo.consName}</li>
                            <li class="list-group-item">问题描述:&nbsp;&nbsp;${consInfo.questionInfo}</li>
                            <li class="list-group-item">诊断答复：${consInfo.answer == null || consInfo.answer == ''? '暂无':consInfo.answer}</li>
                            <li class="list-group-item">答复时间:&nbsp;&nbsp;${consInfo.answerTime == null || consInfo.answerTime == ''? '暂无':consInfo.answerTime}</li>
                            <li class="list-group-item">评价内容:&nbsp;&nbsp;${consInfo.evaluate == null || consInfo.evaluate == ''? '暂无评价':consInfo.evaluate}</li>
                        </ul>
                `
            $('#sub-info-div')[0].innerHTML = str;
        },
        error:function() {
        }
    });
}

function sub(id,subStatus){
    console.log(id);
    if (subStatus == '已预约'){
        $.ajax({
            url:config.url + "/order/changeConfirm",
            dataType:'json',
            xhrFields: {
                withCredentials: true
            },
            data:{
                id:id
            },
            crossDomain: true,
            type:'GET',
            success:function() {
                alert('确认预约成功');
                location.reload();
            },
            error:function() {
            }
        });
    }else {
        $(".sub-btn").attr('data-toggle', 'modal');
        $(".sub-btn").attr('data-target', '#myModal3');
        console.log(dataArr);
        var str2 = '';
        for (let i = 0; i < dataArr.length; i++) {
            let a = dataArr[i];
            if (a.id == id) {

                str2 +=
                    `<ul class="list-group">
                        <li class="list-group-item">咨询人:&nbsp;&nbsp;${a.uusername}</li>
                        <li class="list-group-item">领域:&nbsp;&nbsp;${a.fieldName}</li>
                        <li class="list-group-item">预约时间:&nbsp;&nbsp;${a.subTime}</li>
                        <li class="list-group-item">问题描述:&nbsp;&nbsp;${a.questionInfo}</li>
                    </ul>
                    <div class="answer-div" id="answer-inp">
                        我的答复
                    </div>
    
                    <div class="input-control has-label-left">
                        <input id="inputAccountExample3" type="text" class="form-control" placeholder="">
                        <label for="inputAccountExample3" 
                                id="answer-inp1" 
                                class="input-control-label-left text-right">内容:</label>
                    </div>`
                break;
            }
        }

        $('#diagnosis-form')[0].innerHTML = str2;
        $('.answer-div').val(id);

    }
}

function submitDiaForm(){
    console.log($('#answer-inp1').val())
    $.ajax({
        url:config.url + "/order/diagnosis",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            id:$('.answer-div').val(),
            answer:$('#inputAccountExample3').val()
        },
        crossDomain: true,
        type:'POST',
        success:function(resp) {
            alert(resp.msg);
            location.reload();
        },
        error:function() {
        }
    });
}

queryBtn(1);
function queryBtn(i){
    let parentElement = parent.document;
    let username = parentElement.getElementById('top-manager1').innerText
    let st = $("#start-time").val();
    let et = $("#end-time").val();
    let orderStatus = $("#picker3").val();
    $.ajax({
        url:config.url + "/order/getList",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            subTime:st.split('T')[0],
            answerTime:et.split('T')[0],
            subStatus:orderStatus,
            consName:username,
            pageNo:i
        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            var totalCount = resp.data.totalCount;
            renderSubscribeList(resp.data.list);
            dataArr = resp.data.list;
            initPage(resp.pageNo,totalCount,resp.data.list);
        },
        error:function() {
        }
    });
}

function onDetermine(){
    let val = $(".modal-body-p").val();
    console.log(val);
    $.ajax({
        url:config.url + "/updateStatus",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{
            id:$(".modal-body-p").val()
        },
        crossDomain: true,
        type:'GET' ,
        success:function(resp) {
            location.reload(true);
        },
        error:function() {
        }
    });
}


$(document).ready(function() {
    $("#input-check").on('click', function(){
        clickSwitch()
    });

    var clickSwitch = function() {
        if ($("#input-check").is(':checked')) {
            $("#check-name")[0].innerHTML = "封禁";
        } else {
            $("#check-name")[0].innerHTML = "开启";
        }
    };
});

function removeI(){
    $('#use-input').val("");
}

getpt()
function getpt(){
    $.ajax({
        url:config.url + "/order/getselect",
        dataType:'json',
        xhrFields: {
            withCredentials: true
        },
        data:{

        },
        crossDomain: true,
        type:'GET',
        success:function(resp) {
            renderSelect(resp.data);
        },
        error:function() {
        }
    });
}

function renderSelect(arr){
    let str = ' <option value="">请选择</option>';
    for (let i = 0; i < arr.length; i++) {
        let a  = arr[i];
        str += `<option value="${a.id}">${a.status}</option>`;
    }
    $('#picker3')[0].innerHTML = str;
}